Helpful Information
Home
Categories
Contact us
Category: Regex Programming
Matching a <div> without another div inside it
I'm working on HTML (I can't control it so can't parse it as XML as it wont always be valid) .
I want to match (for example) any div that does not have another div inside it:
(the bold parts here)
<div>
<div>
<div>Foo</div>
</div>
</div>
<div />
<div>
<span>Bar</span>
</div>
This works:
/<(div)([^>]*)(\/>|>((?!<[\/]*\1>).?)*?)<\/\1>/s
on strings up to around 5000 chars, then appears to cause php to crash. It also seems overy complicated for what i'm trying to do.
Is there another easier way?
Thanks :)
This may perform better:
'/<div(?:[^>]*\/>|>(?:(?!<\/?div>).)*<\/div>)/s'
privacy (GDPR)